﻿new Module('CobraCms.Js.Application', function () {
    var Class = Phosphor.Utilities.Class;

    var ApplicationUi = new Class({

        initialize: function (options) {
            this.InitializeActionDialog();
        },

        InitializeActionDialog: function () {
            $("#workspace-dialog").dialog({
                autoOpen: false,
                resizable: false,
                draggable: false,
                height: 768,
                width: 1024,
                modal: true
            });
        },

        OpenActionDialog: function (url, onLoaded) {
            $.ajax(
                {
                    type: "GET",
                    url: url,
                    success: function (data) {
                        $("#workspace-zone").html(data);
                        $("#workspace-dialog").dialog("open");
                        if (onLoaded) {
                            onLoaded();
                        }
                    },
                    error: function (jqHR, textStatus, errorThrown) {
                        alert("Failed: " + textStatus);
                    }
                });
        },

        InitiateTreeAction: function (jsTreeNode, treeAction) {
            if (treeAction = 'Refresh') {
                alert(jsTreeNode.data.key);
                jsTreeNode.reloadChildren(function (node, isOk) {
                    if (!isOk) alert("Node " + node + " could not be reloaded.");
                });
            }
            else {
                var contentNode = jsTreeNode.data.contentNode;
                this.OpenActionDialog('/content/nodes/' + contentNode.NodeId + '/' + treeAction.ActionKey);
            }
        },

        PerformUiTasks: function (taskList) {
            if (taskList) {
                taskList.forEach(function (task, index) {
                    if (task.TaskType = "ReloadNodeChildren") {
                        alert("Loading children for node " + task.NodeId);
                    }
                });
            }
        }
    });

    this.ApplicationUi = ApplicationUi;
    this.Instance = new CobraCms.Js.Application.ApplicationUi();
});
